import requests
from pyquery import PyQuery as pq
import re
import pandas
from tqdm import tqdm

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.50 '
}


def get_response(html_url):
    response = requests.get(url=html_url, headers=header, timeout=15)
    response.encoding = response.apparent_encoding
    return response


def save(name, title, content):
    with open(name + '.txt', mode='a', encoding='utf-8')as f:
        f.write(title)
        f.write('\n')
        f.write(content)
        f.write('\n')


def get_novel_url(html_url):
    response = get_response(html_url)
    selector = pq(response.text)
    name = selector('#info h1').text()
    hrefs = selector('#list dd a').items()
    for index in tqdm(hrefs):
        href = 'http://www.beqege.cc' + index.attr.href
        response = get_response(href)
        selector = pq(response.text)
        title = selector('.bookname h1').text()
        content_list = selector('#content').text()
        content = "\t" + re.sub(pattern='[\n]', string=content_list, repl="\n\t")
        save(name, title, content)
    print("下载完成")


def Search_book():
    while 1:
        print("输入0退出")
        book = input('请输入书名或者作者名:')
        if book == '0':
            break
        search_url = 'http://www.beqege.cc/search.php'
        data = {
            'keyword': book
        }
        # 根据书名book爬取相关书以及url和作者
        response_book = requests.post(url=search_url, data=data, headers=header, timeout=15)
        response_book.encoding = response_book.apparent_encoding
        selector_book = pq(response_book.text)
        # 选择<ul class="chapter-list">
        book_list = selector_book('.chapter-list li')
        if book_list:
            List = []
            # 对每本书进行遍历
            for books in book_list.items():
                # 小说url
                novel_id = books('.s2 a').attr.href
                # 小说名字
                novel_name = re.sub(pattern='[\n]', string=books('.s2 a').text(), repl="")
                # 小说作者
                author = re.sub(pattern='[\n]', string=books('.s4').text(), repl="")
                Dict = {
                    '小说名字': novel_name,
                    '小说作者': author,
                    '小说地址': novel_id
                }
                List.append(Dict)
            print(f'一共搜索到{len(List)}个数据')
            search_result = pandas.DataFrame(List)
            print(search_result)
            book_num = input("请输入要下载的小说序号:")
            book_url = List[int(book_num)]["小说地址"]
            get_novel_url(book_url)
        else:
            print("没有搜索结果")


if __name__ == '__main__':

    Search_book()
